package com.dimples.wxpush.helper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.dimples.wxpush.constant.QiYeWxConfigConstant;
import com.dimples.wxpush.entity.QyWxImageTextMessage;
import com.dimples.wxpush.enums.QyWxClickEnum;
import com.dimples.wxpush.enums.QyWxColorEnum;
import com.dimples.wxpush.enums.TypeEnum;
import com.dimples.wxpush.utils.HttpUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/11/15
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class QyWxHelper {

    private final QiYeWxConfigConstant qiYeWxConfigConstant;

    public String getQyWxAccessToken() {
        //封装请求数据
        String params = "corpid=" + qiYeWxConfigConstant.getCorpid() + "&corpsecret=" + qiYeWxConfigConstant.getCorpsecret();
        //发送GET请求
        String sendGet = HttpUtils.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken", params);
        // 解析相应内容（转换成json对象）
        JSONObject jsonObject1 = JSONObject.parseObject(sendGet);
        log.info("微信token响应结果=" + jsonObject1);
        //拿到access-token
        String accessToken = StrUtil.toStringOrNull(jsonObject1.get("access_token"));
        if (!StringUtils.hasText(accessToken)) {
            throw new RuntimeException("token获取失败，请检查：公众号的，appId、appSecret是否输入正确，务必重刷浏览器重新复制");
        }
        return accessToken;
    }

    public QyWxImageTextMessage createImageTextMessage() {
        QyWxImageTextMessage imageTextMessage = QyWxImageTextMessage.builder()
                .agentid(qiYeWxConfigConstant.getAgentId())
                .touser("@all").toparty("@all").totag("@all")
                .msgtype(TypeEnum.template_card.name())
                .agentid(qiYeWxConfigConstant.getAgentId())
                .enable_id_trans(0).enable_duplicate_check(0).duplicate_check_interval(1800)
                .build();

        QyWxImageTextMessage.TemplateCardBean templateCardBean = QyWxImageTextMessage.TemplateCardBean.builder()
                .card_type(TypeEnum.news_notice.name())
                .task_id(IdUtil.getSnowflakeNextIdStr())
                .build();

        templateCardBean.setSource(QyWxImageTextMessage.TemplateCardBean.SourceBean.builder()
                .icon_url(StrUtil.format("https://dailybing.com/api/v1/{}zh-cnFHD1360", DateUtil.format(DateUtil.yesterday(), DatePattern.PURE_DATE_PATTERN)))
                .desc("♥是爱蛋娃的一天哦!️♥")
                .desc_color(QyWxColorEnum.BLACK.getColor())
                .build());
        templateCardBean.setAction_menu(QyWxImageTextMessage.TemplateCardBean.ActionMenuBean.builder()
                .desc("操作菜单")
                .action_list(CollUtil.newArrayList(
                        QyWxImageTextMessage.TemplateCardBean.ActionMenuBean.ActionListBean.builder()
                                .text("欢迎归来呀")
                                .key("welcome_back")
                                .build(),
                        QyWxImageTextMessage.TemplateCardBean.ActionMenuBean.ActionListBean.builder()
                                .text("再见啦")
                                .key("good_bye")
                                .build()
                )).build());
        ChineseDate chineseDate = new ChineseDate(DateUtil.date());
        templateCardBean.setMain_title(QyWxImageTextMessage.TemplateCardBean.MainTitleBean.builder()
                .title(StrUtil.format("{} {} {}"
                        , DateUtil.format(DateUtil.date(), DatePattern.CHINESE_DATE_PATTERN)
                        , DateUtil.thisDayOfWeekEnum().toChinese()
                        , chineseDate.getChineseMonthName() + chineseDate.getChineseDay() + StrUtil.SPACE + chineseDate.getFestivals()
                ))
                .build());
        templateCardBean.setCard_image(QyWxImageTextMessage.TemplateCardBean.CardImageBean.builder()
                .url(StrUtil.format("https://dailybing.com/api/v1/{}zh-cnFHD1360", DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN)))
                .aspect_ratio(1.3)
                .build());
        templateCardBean.setImage_text_area(
                QyWxImageTextMessage.TemplateCardBean.ImageTextAreaBean.builder()
                        .type(QyWxClickEnum.URL.getClick())
                        .url("http://dawaxiaowo.online/Love.html")
                        .image_url(StrUtil.format("https://dailybing.com/api/v1/{}zh-cnFHD1360", DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -2), DatePattern.PURE_DATE_PATTERN)))
                        .build());

        templateCardBean.setCard_action(QyWxImageTextMessage.TemplateCardBean.CardActionBean.builder()
                .type(QyWxClickEnum.URL.getClick())
                .url("http://dawaxiaowo.online/Love.html")
                .build()
        );

        imageTextMessage.setTemplate_card(templateCardBean);
        return imageTextMessage;
    }

}




























